package com.mashibing.juc.c_026_00_interview.A1B2C3;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Arrays;
import java.util.stream.Stream;

public class T10_00_PipedStream2 {

    public static void main(String[] args) throws Exception {

        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();

        PipedInputStream input1 = new PipedInputStream();
        PipedInputStream input2 = new PipedInputStream();
        PipedOutputStream output1 = new PipedOutputStream();
        PipedOutputStream output2 = new PipedOutputStream();

        input1.connect(output1);
        input2.connect(output2);

        new Thread(() -> {
            byte[] buffer = new byte[1];
            try {
                for (char i : aI) {
                    output2.write(i);
                    input1.read(buffer);
                    System.out.printf("\033[34m-%s-\033[0m", new String(buffer));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }, "t1").start();

        new Thread(() -> {
            byte[] buffer = new byte[1];
            try {
                for (char c : aC) {
                    input2.read(buffer);
                    System.out.printf("\033[33m-%s-\033[0m", new String(buffer));
                    output1.write(c);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "t2").start();

    }

}


